package com.park.utils.distance;

import cn.hutool.core.util.ObjectUtil;

/**
 * @author Administrator
 * @title: LongLatDistance
 * @projectName park
 * @description: 距离公式
 * @date 2020/5/221:52
 */
public class LongLatDistanceUtil {

    /**
     　　* @description:  计算两个经纬度之间的距离
     　　* @param [lat1纬度, lon1经度, lat2纬度, lon2 经度]
     　　* @return {@link double}
     　　* @throws
     　　* @author ZhangHaiWen
     　　* @version 2.0
     　　* @date 2020/5/2 21:53
     　　*/
    public static double distance(double lat1, double lon1,double lat2, double lon2) {
        if(ObjectUtil.isEmpty(lat1) || ObjectUtil.isEmpty(lon1) || ObjectUtil.isEmpty(lat2)||ObjectUtil.isEmpty(lon2)){
            return 1001d;
        }
        final int R = 6371; // 地球半径
        double latDistance = Math.toRadians(lat2 - lat1);
        double lonDistance = Math.toRadians(lon2 - lon1);
        double a = Math.sin(latDistance / 2) * Math.sin(latDistance / 2)
                + Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2))
                * Math.sin(lonDistance / 2) * Math.sin(lonDistance / 2);
        double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
        double distance = R * c * 1000; // 单位转换成米
        distance = Math.pow(distance, 2);
        return Math.sqrt(distance);
    }
}
